package com.yeyaomai.dksns.util;

import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
/**
 * 接受和处理http请求类 包含post和get请求
 * @author zuoshuaiok@gmail.com
 *
 * 2012-11-1
 */
public class HttpUtil {

	/**
	 * @param url
	 * @param queryString
	 *            类似a=b&c=d 形式的参�?
	 * @param inputObj
	 *            发�?�到服务器的对象�?
	 * @return 服务器返回到客户端的对象,发生异常时�?�返�? null 修改�?:杨小�? 2010-5-27
	 */
	public Object getObjFromServer(String url, String queryString,
			Object inputObj) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(url);
		post.setQueryString(queryString);
		post.setRequestHeader("Content-Type", "application/octet-stream");
		java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream(
				1024);
		java.io.ByteArrayInputStream bInput = null;
		java.io.ObjectOutputStream out = null;
		Serializable returnObj = null;
		try {
			out = new java.io.ObjectOutputStream(bOut);
			out.writeObject(inputObj);
			out.flush();
			out.close();
			out = null;
			bInput = new java.io.ByteArrayInputStream(bOut.toByteArray());
			RequestEntity re = new InputStreamRequestEntity(bInput);
			post.setRequestEntity(re);
			client.executeMethod(post);
			java.io.InputStream in = post.getResponseBodyAsStream();
			java.io.ObjectInputStream oInput = new java.io.ObjectInputStream(in);
			returnObj = (Serializable) oInput.readObject();
			oInput.close();
			oInput = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			returnObj = null;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			returnObj = null;
		} finally {
			try {
				if (out != null) {
					out.close();
					out = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (bInput != null) {

					bInput.close();
					bInput = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			// 释放连接
			post.releaseConnection();
		}
		return returnObj;
	}

	/**
	 * @param url
	 * @param queryString
	 *            类似a=b&c=d 形式的参�?
	 * @return 服务端返回的结果�?
	 * @throws IOException
	 */
	public String getSendHttp(String url, String queryString)
			throws IOException {
		HttpClient client = new HttpClient();
		GetMethod get = new GetMethod(url);
		get.setQueryString(queryString);
		get.setRequestHeader("Content-Type", "application/octet-stream");
		String returnStr = null;
		try {
			client.executeMethod(get);
			returnStr = get.getResponseBodyAsString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放连接
			get.releaseConnection();
		}
		return returnStr;
	}

	/**
	 * @param url
	 * @param queryString
	 *            类似a=b&c=d 形式的参�?
	 * @return 服务端返回的结果�?
	 * @throws IOException
	 */
	public String postSendHttp(String url, String queryString, String bodyString)
			throws IOException {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(url);
		// post.setQueryString(queryString);
		// post.setRequestHeader("Content-Type", "application/octet-stream");
		// post.setRequestHeader("CharacterEncoding", "UTF-8");
		// post.setRequestBody(bodyString);
		String returnStr = null;
		try {
			client.executeMethod(post);
			returnStr = post.getResponseBodyAsString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放连接
			post.releaseConnection();
		}
		return returnStr;
	}
	public static void main(String[] args) throws IOException {
//		System.out.println(new HttpUtil().getSendHttp("http://www.baidu.com", null));
	}
}
